import options from './options.js';
			
class BlueTooth {
	constructor(arg) {
		if(plus){
			this.ProcessMian = plus.android.runtimeMainActivity();
			this.BluetoothAdapter = plus.android.importClass('android.bluetooth.BluetoothAdapter');
			this.BluetoothDevice = plus.android.importClass('android.bluetooth.BluetoothDevice');
			this.mBluetoothAdapter = this.BluetoothAdapter.getDefaultAdapter();
		}
		
		this.options = options;
		this.data_connect = [];
		this.device_address = "";
		this.eChartsVal = null;
	}
	
	broadcastReceiver(){
		this.receiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {
			onReceive: (context, intent) => {
				//实现onReceiver回调函数
				plus.android.importClass(intent); //通过intent实例引入intent类，方便以后的‘.’操作
				/**
				 * android.bluetooth.device.action.FOUND
				 * android.bluetooth.adapter.action.DISCOVERY_FINISHED
				 */
				const action_state = intent.getAction(); //android.bluetooth.device.action.FOUND
				if (action_state == 'android.bluetooth.adapter.action.DISCOVERY_FINISHED') {
					this.ProcessMian.unregisterReceiver(receiver); //取消监听
					console.log('==============搜索结束==============');
				} else {
					const BleDevice = intent.getParcelableExtra(this.BluetoothDevice.EXTRA_DEVICE);
					// 判断是否配对过: this.BluetoothDevice.BOND_NONE---10
					if (BleDevice.getBondState() == this.BluetoothDevice.BOND_NONE) {
						// name-deviceId
						let device_name = BleDevice.getName() || 'NULL';
						let device_address = BleDevice.getAddress();
						console.log('未配对蓝牙设备：' + device_name + '    ' + device_address);
						if (this.data_connect.find(val => val.deviceId == device_address)) {
						} else {
							this.data_connect.push({
								name: device_name,
								device_address: device_address
							});
						}
					} else {
						console.log('已配对蓝牙设备：' + BleDevice.getName() + '    ' + BleDevice.getAddress());
					}
				}
			}
		});
	}
	
	inputStreamNotify(fnCallback){
		if (this.bluetoothSocket && this.bluetoothSocket.isConnected()) {
			plus.android.importClass(this.bluetoothSocket);
			let inputStream = this.bluetoothSocket.getInputStream();
			plus.android.importClass(inputStream);
			const BufferedReader = plus.android.importClass('java.io.BufferedReader');
			const InputStreamReader = plus.android.importClass('java.io.InputStreamReader');
		
			let echarts = [],
				message = '';
			try {
				const inputStreamReader = new InputStreamReader(inputStream);
				const buffer = new BufferedReader(inputStreamReader);
		
				this.eChartsVal = setInterval(() => {
					console.log('this is message:');
					// let byte = inputStream.read();
					// message += byte ? String.fromCharCode(byte) : "";
					message = buffer.readLine();
					// console.log(message);
					if (message) {
						fnCallback(message);
					}else{
						fnCallback(404);
					};
				}, this.targetSpace || 100);
			} catch (e) {
				console.log(e);
			}
		} else {
			uni.showToast({
				title: '请选择蓝牙设备',
				duration: 2000,
				icon: 'none'
			});
		}
	}
	
	connectState(){
		const deviceId = this.device_address || '20:20:03:27:31:74';
		
		// getApp().globalData.connect_save = deviceId;
		if (deviceId) {
			this.main = plus.android.runtimeMainActivity();
			const UUID = plus.android.importClass('java.util.UUID');
			const uuid = UUID.fromString('00001101-0000-1000-8000-00805F9B34FB');
			const device = this.mBluetoothAdapter.getRemoteDevice(deviceId);
			plus.android.importClass(device);
			this.bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid);
			plus.android.importClass(this.bluetoothSocket);
		
			if (this.bluetoothSocket.isConnected()) {
				uni.showToast({
					title: '蓝牙连接成功',
					duration: 2000,
					icon: 'success'
				});
			} else {
				uni.showLoading({
					title: '正在尝试连接....'
				});
				this.bluetoothSocket.connect();
				const timer = setInterval(() => {
					if (this.bluetoothSocket.isConnected()) {
						uni.hideLoading();
						clearInterval(timer);
					} else {
						this.bluetoothSocket.connect();
					}
				});
			}
		} else {
			uni.showModal({
				title: '提示',
				content: '请选择蓝牙设备',
				showCancel: false
			});
		}
	}
	
	clearChartsVal(){
		return new Promise((resolve,reject)=>{
			if(this.bluetoothSocket.isConnected()){
				this.bluetoothSocket.close();
			}
			if(this.eChartsVal){
				clearInterval(this.eChartsVal);
				this.eChartsVal = null;
			}
			resolve(200 || true);
		})
	}
}


export default BlueTooth;
